# 生成词法分析器和语法分析器
find_package(FLEX REQUIRED)
find_package(BISON REQUIRED)

# 设置Bison和Flex的输出路径
set(BISON_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(FLEX_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})

# 生成语法分析器
bison_target(
    Parser 
    ${CMAKE_CURRENT_SOURCE_DIR}/parser.y 
    ${BISON_OUTPUT_DIR}/parser.tab.cpp
    DEFINES_FILE ${BISON_OUTPUT_DIR}/parser.tab.hpp
    COMPILE_FLAGS "-Wno-conflicts-sr -Wno-other"
)

# 生成词法分析器
flex_target(
    Lexer 
    ${CMAKE_CURRENT_SOURCE_DIR}/lexer.l 
    ${FLEX_OUTPUT_DIR}/lexer.cpp
)

# 显式声明依赖关系
add_flex_bison_dependency(Lexer Parser)

# 创建库
add_library(lex_project
    ${CMAKE_CURRENT_SOURCE_DIR}/ast.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/semantic.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/codegenerator.cpp
    ${BISON_Parser_OUTPUTS}
    ${FLEX_Lexer_OUTPUTS}
)

# 设置包含目录
target_include_directories(lex_project 
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
    PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
)

# 设置C++标准
target_compile_features(lex_project PRIVATE cxx_std_17)

# 添加编译定义解决 EOF 冲突
target_compile_definitions(lex_project PRIVATE YY_NO_UNISTD_H)